// pages/user/index.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    token: '',
    list: '',
    rank: [],
    performance: '',
    status: "",
    pid: ""
  },
  logout() {
    let data = {
      method: 'user.logout',
      token: wx.getStorageSync('token')
    }
    app.api.post('/api/index.html', data).then((res) => {
      if (res.status) {
        wx.removeStorageSync('token')
        wx.removeStorageSync('id');
        wx.removeStorageSync('share');
        wx.redirectTo({
          url: '/pages/user/index',
        })
      }
    })
  },
  service() {
    wx.navigateTo({
      url: '/pages/service/index',
    })
  },
  teacher() {
    wx.navigateTo({
      url: '/pages/myTeacher/index?id=' + this.data.list.teacher_id,
    })
  },
  getUserProfile: function (e) {
    wx.getUserProfile({
      desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res_user) => {
        if (wx.getStorageSync('code')) {
          wx.showLoading({
            title: '登陆中',
          })
          let data = {
            code: wx.getStorageSync('code'),
            method: 'user.minilogin',
            iv: res_user.iv,
            edata: res_user.encryptedData
          }
          app.api.post('/api/index.html', data).then(res => {
            if (res.status) {
              wx.hideLoading()
              wx.showToast({
                title: res.msg,
                icon: 'none'
              })
              setTimeout(() => {
                wx.setStorageSync('token', res.data.token)
                console.log(wx.getStorageSync('share'))
                if (wx.getStorageSync('share') == '1') {
                  wx.reLaunch({
                    url: '/pages/invite/index?id=' + wx.getStorageSync('id'),
                  })
                } else if (this.data.status == '1') {//1 直播
                  wx.reLaunch({
                    url: '/pages/liveDetails/index?id=' + wx.getStorageSync('classId') + '&pid=' + this.data.pid,
                  })
                } else if (this.data.status == '2') {//2 直播拼团
                  wx.reLaunch({
                    url: '/pages/livePt/index?id=' + wx.getStorageSync('classId') + '&pid=' + this.data.pid,
                  })
                } else if (this.data.status == '3') {//3 面授
                  wx.reLaunch({
                    url: '/pages/faceDetails/index?id=' + wx.getStorageSync('classId') + '&pid=' + this.data.pid,
                  })
                } else if (this.data.status == '4') {//4 面授拼团
                  wx.reLaunch({
                    url: '/pages/facePt/index?id=' + wx.getStorageSync('classId') + '&pid=' + this.data.pid,
                  })
                } else if (this.data.status == '5') {//5 录播
                  wx.reLaunch({
                    url: '/pages/recDetails/index?id=' + wx.getStorageSync('classId') + '&pid=' + this.data.pid,
                  })
                } else if (this.data.status == '6') {//6 录播拼团
                  wx.reLaunch({
                    url: '/pages/recPt/index?id=' + wx.getStorageSync('classId') + '&pid=' + this.data.pid,
                  })
                } else {
                  wx.reLaunch({
                    url: '/pages/user/index',
                  })
                }
              }, 500)
            } else {
              wx.showToast({
                title: res.msg,
                icon: 'none'
              })
            }
          })
          //发起网络请求
        }
      }
    })
  },
  withdraw() {
    wx.navigateTo({
      url: '/pages/withdraw/index?money=' + this.data.list.commission,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (!wx.getStorageSync('token')) {
      wx.login({
        success: (res) => {
          wx.setStorageSync('code', res.code)
        }
      })
    }
    this.setData({
      token: wx.getStorageSync('token'),
      status: options.status,//海报分享状态
      pid: options.pid//海报分享人id
    })
  },
  getList() {
    let data = {
      method: 'user.info',
      token: wx.getStorageSync('token')
    }
    app.api.post('/api/index.html', data).then((res) => {
      if (res.status) {
        this.setData({
          list: res.data,
        })
      }
    })
  },
  getRank() {
    let data = {
      method: 'user.gettopten',
    }
    app.api.post('/api/index.html', data).then((res) => {
      if (res.status) {
        this.setData({
          rank: res.data,
        })
      }
    })
  },
  getPerformance() {
    let data = {
      method: 'user.getquarter',
      token: wx.getStorageSync('token')
    }
    app.api.post('/api/index.html', data).then((res) => {
      if (res.status) {
        this.setData({
          performance: res.data,
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getRank()
    if (wx.getStorageSync('token')) {
      this.getList()
      this.getPerformance()
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})